package operator

import (
	"github.com/go-logr/logr"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/kubernetes"
	myErrors "middleware.io/nacos/pkg/errors"
	"middleware.io/nacos/pkg/service/k8s"

	nacosv1beta1 "middleware.io/nacos/api/v1beta1"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

type OperatorClient struct {
	KindClient *KindClient
}

func NewOperatorClient(logger logr.Logger, clientset *kubernetes.Clientset, scheme *runtime.Scheme, client client.Client) *OperatorClient {
	service := k8s.NewK8sService(clientset, logger)
	return &OperatorClient{
		// resource client
		KindClient: NewKindClient(logger, service, scheme),
	}
}

func (c *OperatorClient) PreCheck(nacos *nacosv1beta1.Nacos) {

}

func (c *OperatorClient) MakeEnsure(nacos *nacosv1beta1.Nacos) {
	// 验证字段
	c.KindClient.ValidationField(nacos)
	switch nacos.Spec.Type {
	case TypeStandAlone:
		c.KindClient.EnsureConfigmap(nacos)
		c.KindClient.EnsureStatefulSet(nacos)
		c.KindClient.EnsureService(nacos)
	case TypeCluster:
		c.KindClient.EnsureConfigmap(nacos)
		c.KindClient.EnsureStatefulSetCluster(nacos)
		c.KindClient.EnsureHeadlessServiceCluster(nacos)
		c.KindClient.EnsureClientService(nacos)
	default:
		panic(myErrors.New(myErrors.CODE_PARAMETER_ERROR, myErrors.MSG_PARAMETER_ERROT, "nacos.Spec.Type", nacos.Spec.Type))
	}
}
